package com.yunhe.springbootdemo3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.springbootday1.dao.PostDao;
import com.yunhe.springbootday1.domain.Post;
import com.yunhe.springbootday1.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostDao postDao;


    /**
     * 分页查询所有帖子
     * @param pageNum
     * @return
     */
    @Override
    public Map<String, Object> allPosts(int pageNum) {

        //设置查询的页
        PageHelper.startPage(pageNum,5);

        //执行查询
        List<Post> postsList = postDao.allPosts();

        //获取分页信息
        PageInfo<Post> info = new PageInfo(postsList);

        //获取帖子总数
        long total = info.getTotal();

        //创建一个map
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("postsList",postsList);
        map.put("count",total);

        return map;
    }

    @Override
    public Post getPostById(int postId) {
        return postDao.getPostById(postId);
    }
}
